package com.example.provider.service.Impl;
import com.example.provider.mapper.TK_DeptMapper;
import com.example.provider.mapper.TK_SalaryMapper;
import com.example.provider.mapper.TK_UserMapper;
import com.guo.constant.common;
import com.guo.pojo.*;
import com.guo.service.TK_UserService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
@Service
public class TK_UserServiceImpl implements TK_UserService {

    @Autowired
    private TK_UserMapper mapper;

    @Autowired
    private TK_DeptMapper tk_deptMapper;

    @Autowired
    private TK_SalaryMapper tk_salaryMapper;


    @Override
    public Staff_Salary_Dept getStaffInfo(String name) {
        // 联表查询通过查询多个单表实现
        // 找到员工信息
        Example example = new Example(Staff.class);
        example.createCriteria().andEqualTo("name",name);
        List<Staff> staffList = mapper.selectByExample(example);
        Staff staffInfo = staffList.get(0);

        // 通过员工信息找到对应的部门
        Example example1 = new Example(Department.class);
        example1.createCriteria().andEqualTo("departmentId",staffInfo.getDepartmentId());
        List<Department> deptList = tk_deptMapper.selectByExample(example1);
        Department deptInfo = deptList.get(0);

        // 通过员工信息找到对应的薪资
        /*Example example2 = new Example(Salary.class);
        example2.createCriteria().andEqualTo("salaryId",staffInfo.getSalaryId());
        List<Salary> salaryList = tk_salaryMapper.selectByExample(example2);
        Salary salaryInfo = salaryList.get(0);*/

        return new Staff_Salary_Dept(null,name,deptInfo.getDepartment(),null);
    }

    @Override
    public String addStaff(Staff staff) {
        mapper.insertSelective(staff);
        return common.ADD_SUCCESS;
    }

    @Override
    public String updateStaff(Staff staff) {
        Example example = new Example(Staff.class);
        example.createCriteria().andEqualTo("id",staff.getId());
        mapper.updateByExample(staff,example);
        return common.UPDATE_SUCCESS;
    }

    @Override
    public String deleteStaff(Integer id) {
        Example example = new Example(Staff.class);
        example.createCriteria().andEqualTo("id",id);
        mapper.deleteByExample(example);
        return common.DELETE_SUCCESS;
    }
}
